
package ParseURL;
import java.io.File;
import java.io.IOException;
import java.util.Locale;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


public class WriteToExcel {

	private WritableCellFormat timesBoldUnderline;
	private WritableCellFormat times;
	private String inputFile;
	WritableSheet excelSheet;
	WritableWorkbook workbook;
	
public void setOutputFile(String inputFile) {
	this.inputFile = inputFile;
	}

	public void write() throws IOException, WriteException {
		File file = new File(inputFile);
		WorkbookSettings wbSettings = new WorkbookSettings();

		wbSettings.setLocale(new Locale("en", "EN"));

		this.workbook = Workbook.createWorkbook(file, wbSettings);
		this.workbook.createSheet("Report",0);
		this.excelSheet = workbook.getSheet(0);
		createLabel(this.excelSheet);
	}
		
		
		
		public void writeData(String URL, String Category, int rownum) throws IOException, WriteException 
		{
		createLabel(this.excelSheet);
		createContent(URL, Category, rownum);

		this.workbook.write();
		System.out.println("Written to excel");
		//this.workbook.close();
		System.out.println("Closed excel");
		}

	
	//public addData()
	
	private void createLabel(WritableSheet sheet)
			throws WriteException {
		// Lets create a times font
		WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
		// Define the cell format
		times = new WritableCellFormat(times10pt);
		// Lets automatically wrap the cells
		times.setWrap(true);

		// Create create a bold font with unterlines
		WritableFont times10ptBoldUnderline = new WritableFont(
				WritableFont.TIMES, 10, WritableFont.BOLD, false,
				UnderlineStyle.SINGLE);
		timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
		// Lets automatically wrap the cells
		timesBoldUnderline.setWrap(true);

		CellView cv = new CellView();
		cv.setFormat(times);
		cv.setFormat(timesBoldUnderline);
		cv.setAutosize(true);

		// Write a few headers
		//addCaption(sheet, 0, 0, "Name");
		addCaption(sheet, 0, 0, "URL");
		addCaption(sheet, 1, 0, "Category");
		//addLabel(1, 2, "a", times);
		

	}

	 void createContent(String URL, String Category, int rownum) throws WriteException,
			RowsExceededException {
		 Label label;
			label = new Label(0, rownum, URL, times);
			this.excelSheet.addCell(label);
			label = new Label(1, rownum, Category, times);
			this.excelSheet.addCell(label);
		 
		    System.out.println("Added" + URL + "   " + Category);
		}
		// Lets calculate the sum of it
	/*	StringBuffer buf = new StringBuffer();
		//buf.append("SUM(A2:A10)");
		Formula f = new Formula(0, 10, buf.toString());
		sheet.addCell(f);
		buf = new StringBuffer();
		//buf.append("SUM(B2:B10)");
		f = new Formula(1, 10, buf.toString());
		sheet.addCell(f);
*/
		// Now a bit of text
	/*	for (int i = 12; i < 20; i++) {
			// First column
			addLabel(sheet, 0, i, "Boring text " + i);
			// Second column
			addLabel(sheet, 1, i, "Another text");
		}*/
	

	private void addCaption(WritableSheet sheet, int column, int row, String s)
			throws RowsExceededException, WriteException {
		Label label;
		label = new Label(column, row, s, timesBoldUnderline);
		sheet.addCell(label);
	}

	/*private void addString(WritableSheet sheet, int column, int row,
			Integer integer) throws WriteException, RowsExceededException {
		//Number number;
		//number = new Number(column, row, integer, times);
		//sheet.addCell(number);
		String string;
		string = new String(column, row, integer, sheet);
		string = new String();
		//sheet.addCell(number);
	}*/

	private void addLabel(WritableSheet sheet, int column, int row, String s)
			throws WriteException, RowsExceededException {
		Label label;
		label = new Label(column, row, s, times);
		sheet.addCell(label);
	}

	/*public static void main(String[] args) throws WriteException, IOException {
		WriteToExcel test = new WriteToExcel();
		test.setOutputFile("C:/Aparna/test.xls");
		test.write();
		System.out.println("Please check the result file under c:/temp/lars.xls ");
	}*/
	
void closeWB() throws IOException, WriteException
{   
	this.workbook.write();
	this.workbook.close();
}
	
	
}